package cn.bdqn.controller;


import cn.bdqn.pojo.AppCategory;
import cn.bdqn.pojo.AppInfo;
import cn.bdqn.pojo.BackendUser;
import cn.bdqn.pojo.DataDictionary;
import cn.bdqn.service.AppCategoryService;
import cn.bdqn.service.AppInfoService;
import cn.bdqn.service.BackendUserService;
import cn.bdqn.service.DataDictionaryService;
import cn.bdqn.tools.PageSupport;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/backendController")
public class BackendController {
    @Resource
    private AppInfoService appInfoService;
    @Resource
    private AppCategoryService appCategoryService;
    @Resource
    private DataDictionaryService dataDictionaryService;


    //三级分类下拉框
    @ResponseBody
    @RequestMapping(value = "/levelCassify", produces = {"application/json;charset=utf-8"})
    public Object levelCassify(@RequestParam(required = false, defaultValue = "1") int parentId) {
        List<AppCategory> category = appCategoryService.getParentId(parentId);
        return JSONArray.toJSONString(category);
    }

    //查询一级分类
    @ResponseBody
    @RequestMapping(value = "/getLevel1", produces = {"application/json;charset=utf-8"})
    public Object getLevel1() {
        List<AppCategory> level1 = appCategoryService.getLevel1();
        return JSON.toJSONString(level1);
    }

    //所属平台
    @ResponseBody
    @RequestMapping(value = "/findApp", produces = {"application/json;charset=utf-8"})
    public Object findApp() {
        List<DataDictionary> app = dataDictionaryService.getDataDictionary();
        return JSONArray.toJSONString(app);
    }

    //分页
    @RequestMapping("/toAppList")
    public String toAppList(HttpServletRequest request, AppInfo appInfo, @RequestParam(defaultValue = "1") Integer pageIndex, Integer parentId) {
        PageSupport<BackendUser> page = new PageSupport();
        page.setCurrentPageNo(pageIndex);
        page.setTotalCount(appInfoService.getAppInfoCount(appInfo));
        List<AppInfo> appInfoList = appInfoService.getAppInfoList(appInfo, (page.getCurrentPageNo() - 1) * page.getPageSize(), page.getPageSize());
        request.setAttribute("appInfoList", appInfoList);
        request.setAttribute("page", page);
        return "/backend/appchecklist";
    }

    //跳转到审核手游页面
    @RequestMapping("/toAppCheckList")
    public String toAppCheckList(HttpSession session, @RequestParam("aid") Integer aid) {
        AppInfo appInfo = appInfoService.getAppInfoId(aid);
        session.setAttribute("appInfo", appInfo);
        return "backend/appcheck";
    }

    //审核通过
    @RequestMapping("/chenkOkNo")
    public String chenkOkNo(@RequestParam("aid") Integer aid, @RequestParam("status") Integer status, @RequestParam("checkId") Integer checkId) {
        int num = appInfoService.updateAppInfo(aid, status, checkId);
        if (num > 0) {
            return "redirect:/backendController/toAppList";
        } else {
            return "/backend/appcheck";
        }
    }
}